var util = require('../../libs/util.js');
var app = getApp();
Page({
  data: {
    historyAddressData:[],
    searchShowed: false,
    searchVal: "",
    selected: false,
    index: ''
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": app.appName +"--历史搜索地址" });
    wx.showToast({
      title: "页面加载中\n请稍候...",
      icon: "loading",
      duration: 5000
    });
    wx.removeStorage({
      key: 'path_shop',
      success: function (res) {
      }
    });
    this.setData({
      index: e.index
    });
    this.loadData();
  },
  onReady: function () {
    wx.hideToast()
  },
  showSearch: function () {
    this.setData({
      searchShowed: true
    });
  },
  hideSearch: function () {
    this.setData({
      searchVal: "",
      searchShowed: false
    });
    this.setDataList("");
  },
  clearSearch: function () {
    this.setData({
      searchVal: ""
    });
    this.setDataList("");
  },
  searchTyping: function (e) {
    this.setData({
      searchVal: e.detail.value
    });
    this.setDataList(e.detail.value);
  },
  loadData() {
    wx.showLoading({
      title: '加载数据中...',
    });
    var that = this;
    wx.getStorage({
      key: 'historyAddress',
      success: function (res) {
        var data = res.data, _data = [];
        if (data) {
          for (var i = 0; i < data.length; i++) {
            _data.push({ name: data[i].name, address: data[i].address, latitude: data[i].latitude, longitude: data[i].longitude,value:i });
          }
        };
        that.setData({
          historyAddressData: _data,
          dataList: _data
        })
        wx.hideLoading();
      }
    });
  },
  setDataList: function (text) {
    var that = this;
    var data = that.data.dataList, _data_ = [];
    for (var i = 0; i < data.length; i++) {
      if (String(data[i].name).indexOf(text) > -1 || String(data[i].address).indexOf(text) > -1) {
        _data_.push({ name: data[i].name, address: data[i].address, latitude: data[i].latitude, longitude: data[i].longitude, value: i });
      }
    };
    that.setData({
      historyAddressData: _data_
    })
  },
  dataSelect: function (e) {
    var historyAddressData = this.data.historyAddressData;
    for (var i = 0; i < historyAddressData.length; i++) {
      historyAddressData[i].checked = historyAddressData[i].value == e.detail.value;
      if (historyAddressData[i].value == e.detail.value) {
        historyAddressData[i].index = this.data.index;
        wx.setStorage({
          key: "path_historyAddress",
          data: historyAddressData[i]
        });
        this.setData({
          selected: true
        })
      }
    }
    this.setData({
      historyAddressData: historyAddressData
    });
  },
  confirmData: function () {
    if (this.data.selected) {
      wx.navigateBack({
        delta: 1
      });
    }
  },
  cancelConfirmData: function () {
    wx.navigateBack({
      delta: 1
    });
  }
});